package net.chrisrichardson.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class MyStreamConsumer implements Runnable {

  private final InputStream inputStream;
  private StringBuffer textBuffer = new StringBuffer();
  
  public MyStreamConsumer(InputStream inputStream) {
    this.inputStream = inputStream;
  }

  public void run() {
    BufferedReader inputStreamReader = new BufferedReader(new InputStreamReader(inputStream));
    String line;
    try {
      while ((line = inputStreamReader.readLine()) != null) {
        System.out.println(line);
        textBuffer.append(line).append('\n');
      }
    } catch (IOException e) {
      e.printStackTrace();
      throw new RuntimeException(e);
    }
  }

  public String getText() {
    return textBuffer.toString();
  }

}
